﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using Redboil.Bss.Model;
using System.Runtime.Serialization.Json;
using System.IO;

namespace Redboil.Bss.Client {
    public partial class SamplePage : Page {
        private WebClient webClient;

        public SamplePage() {
            InitializeComponent();

            webClient = new WebClient();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e) {

        }

        private void buttonCreate_Click(object sender, RoutedEventArgs e) {
            User user = new User();
            user.Account = "Gene";
            user.IsAdministrator = true;
            user.Password = "123456";
            //webClient
        }

        private void buttonRead_Click(object sender, RoutedEventArgs e) {
            webClient.DownloadStringCompleted += (o, ea) => {
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(User));
                Stream memoryStream = new MemoryStream();
                StreamWriter streamWriter = new StreamWriter(memoryStream);
                streamWriter.Write(ea.Result);
                User user = jsonSerializer.ReadObject(memoryStream) as User;
                streamWriter.Close();
                //MessageBox.Show(user.ToString());

                //this.dataGridUser.ItemsSource = user;

                MessageBox.Show(ea.Result);
                //                JavaScriptSerializer jserializer = new JavaScriptSerializer();
                //2 
                //3 User user = jserializer.Deserialize<User>("{\"UserName\":\"user\",\"TrueName\":\"wange\",\"Address\":{\"City\":\"beijing\",\"Country\":\"China\"},\"AddressList\":[{\"City\":\"ShangHai\",\"Country\":\"China\"},{\"City\":\"ShiJiaZhuang\",\"Country\":\"China\"}]}");//将Json字符串转换为实体类

                //            using(Stream responseStream = ea.Result 
                //                response.GetResponseStream())
                //{
                //}


                //this.dataGridUser.ItemsSource = ea.Result;
            };

            webClient.DownloadStringAsync(new Uri("http://localhost:1111/sampleservice"));
        }

        private void buttonUpdate_Click(object sender, RoutedEventArgs e) {
            
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e) {
           
        }

    }
}
